/**
 * 
 */
package com.ufida.ids.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hereway.core.context.PageBean;
import com.hereway.core.dao.IBaseDAO;
import com.hereway.core.exception.ServiceException;
import com.hereway.core.service.BaseServiceImpl;
import com.ufida.ids.dao.IAppDAO;
import com.ufida.ids.model.AppModel;
import com.ufida.ids.model.StaffModel;
import com.ufida.ids.service.IAppService;
import com.ufida.ids.util.Constants;
import com.ufida.ids.util.DateUtil;
import com.ufida.ids.util.ExtUtil;

/**
 * @author kevin
 *
 */
@Service("appService")
public class AppServiceImpl extends BaseServiceImpl<AppModel, Long> implements IAppService {

	@Autowired
	IAppDAO appDAO;
	
	@Override
	protected IBaseDAO<AppModel, Long> getDAO() {
		return appDAO;
	}

	@Override
	public AppModel getByCode(String appCode) throws ServiceException {
		List<AppModel> list = appDAO.find(" where appCode = '" + appCode + "'");
		if(list != null)
			return list.get(0);
		else
			return null;
	}
	
	public List<AppModel> findByIds(String idStr)throws ServiceException{
		String whereStr = " where 1 = 1 and appId in"+ idStr;
		return appDAO.find(whereStr);
	}
	
	public List<AppModel> getAllActiveApp( )throws ServiceException{
		String whereStr = " where 1=1 and entityStatus = "+Constants.STATUS_ACTIVE;
		return appDAO.find(whereStr);
	}
	
	public String getAppListByConPagging(String appName, String url, int pageNum) throws ServiceException{
		
		//分页信息
		PageBean page = new PageBean();
		page.setIndex(pageNum);
		
		String whereStr = getConStr(appName, url);
		//查询条目
		List<AppModel> l = appDAO.find(whereStr, "order by createTs desc", page);
		List countL = appDAO.find(whereStr);
		
		//格式化日期
		for(AppModel app: l){
			app.setCreateTsStr(DateUtil.getDateStrPattern1(app.getCreateTs()));
		}
		String retStr = ExtUtil.getJosonStr(l,countL.size());
		return retStr;
	}
	
	private static String getConStr(String appName, String url){
		StringBuffer whereStr = new StringBuffer("where 1=1 "); 
		if(!"".equals(appName)){
			whereStr.append(" and appName like '%").append(appName).append("%'");
		}
		
		if(!"".equals(url)){
			whereStr.append(" and url like '%").append(url).append("%'");
		}
		
		whereStr.append(" and entityStatus != ").append(Constants.STATUS_DEL);
		
		return whereStr.toString();
	}

}
